package com.ruoyi.edu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.edu.domain.vo.EduCourseVo;
import com.ruoyi.edu.mapper.EduCourseMapper;
import com.ruoyi.edu.service.IEduCourseService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.edu.domain.bo.EduCourseCollectBo;
import com.ruoyi.edu.domain.vo.EduCourseCollectVo;
import com.ruoyi.edu.domain.EduCourseCollect;
import com.ruoyi.edu.mapper.EduCourseCollectMapper;
import com.ruoyi.edu.service.IEduCourseCollectService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 课程收藏Service业务层处理
 *
 * @author ssl
 * @date 2022-08-31
 */
@RequiredArgsConstructor
@Service
public class EduCourseCollectServiceImpl implements IEduCourseCollectService {

    private final EduCourseCollectMapper baseMapper;
private final IEduCourseService eduCourseService;
private final EduCourseMapper eduCourseMapper;
    /**
     * 查询课程收藏
     */
    @Override
    public EduCourseCollectVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询课程收藏列表
     */
    @Override
    public TableDataInfo<EduCourseVo> queryPageList(EduCourseCollectBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EduCourseCollect> lqw = buildQueryWrapper(bo);
        Page<EduCourseCollectVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<EduCourseCollectVo> records = result.getRecords();
        List<EduCourseVo> collect = records.stream().map(item -> {
            EduCourseVo eduCourseVo = eduCourseMapper.selectVoById(item.getCourseId());
            return eduCourseVo;

        }).collect(Collectors.toList());

        List<EduCourseVo> list = eduCourseService.queryListSetValue(collect);

        return TableDataInfo.build(list,result.getTotal());
    }

    /**
     * 查询课程收藏列表
     */
    @Override
    public List<EduCourseCollectVo> queryList(EduCourseCollectBo bo) {
        LambdaQueryWrapper<EduCourseCollect> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EduCourseCollect> buildQueryWrapper(EduCourseCollectBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EduCourseCollect> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCourseId()), EduCourseCollect::getCourseId, bo.getCourseId());
        lqw.eq(StringUtils.isNotBlank(bo.getMemberId()), EduCourseCollect::getMemberId, bo.getMemberId());
        return lqw;
    }

    /**
     * 新增课程收藏
     */
    @Override
    public Boolean insertByBo(EduCourseCollectBo bo) {
        EduCourseCollect add = BeanUtil.toBean(bo, EduCourseCollect.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改课程收藏
     */
    @Override
    public Boolean updateByBo(EduCourseCollectBo bo) {
        EduCourseCollect update = BeanUtil.toBean(bo, EduCourseCollect.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EduCourseCollect entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除课程收藏
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<EduCourseVo> getEduCourseByMemberId(String id) {
        List<EduCourseVo> list = baseMapper.getEduCourseByMemberId(id);
        List<EduCourseVo> list1 = eduCourseService.queryListSetValue(list);
        return list1;
    }
}
